package com.ohuang.demo.page

import android.widget.Toast
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.asFlow
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.transition.Visibility
import com.ohuang.demo.viewModel.TestViewModel
import kotlinx.coroutines.delay

@Composable
fun MainPage() {


    val testViewModel: TestViewModel = viewModel()
    Text(text = "Main testViewModel=" + testViewModel.hashCode())
    val a = remember { mutableStateOf(true) }
    Button(onClick = {
        a.value = !a.value
    }) {
        Text(text = "切换")
    }
    Box {
        VisibilityLayout(boolean = a.value) {
            HomeScreen()
        }
        VisibilityLayout(boolean = !a.value) {
            DetailsScreen()
        }


    }

//    NavHost(navController = navController, startDestination = "home"){
//        composable("home") { HomeScreen(navController) }
//        composable("details") { DetailsScreen(navController) }
//    }

}
@Composable
fun VisibilityLayout(modifier: Modifier= Modifier, boolean: Boolean=true,  content: @Composable () -> Unit){
    Layout(
        content = content,
        modifier = modifier
    ) { measurables, constraints ->
        if (boolean) {
            // 正常布局
            val placeables = measurables.map { it.measure(constraints) }
            layout(constraints.maxWidth, constraints.maxHeight) {
                placeables.forEach { it.placeRelative(0, 0) }
            }
        } else {
            // 隐藏但仍占用空间
            layout(0, 0) {}
        }
    }
}

@Composable
fun HomeScreen() {

    var current = LocalContext.current

    val testViewModel: TestViewModel = viewModel()
    val times = remember {
        mutableStateOf(0)
    }
    LaunchedEffect(Unit) {
        var cout = 60
        while (cout > 0) {
            times.value = cout
            cout--
            delay(1000)
        }

    }

    Column {
        Button(onClick = {
            Toast.makeText(current,"HomeScreen click", Toast.LENGTH_LONG).show()

        }) {
            Text(text = "time=" + times.value)
        }

        Text(text = "vm=" + testViewModel.hashCode())
        Text(text = "LocalViewModelStoreOwner=" + LocalViewModelStoreOwner.current.hashCode())
    }
}

@Composable
fun DetailsScreen() {
    val testViewModel: TestViewModel = viewModel()

    val current = LocalContext.current

    Column {
        Button(onClick = {
            Toast.makeText(current,"DetailsScreen click", Toast.LENGTH_LONG).show()

        }) {
            Text(text = "按钮")
        }
        Text(text = "vm=" + testViewModel.hashCode())
        Text(text = "LocalViewModelStoreOwner=" + LocalViewModelStoreOwner.current.hashCode())
    }

}